package com.neusoft.nep.NEPS.controller;

import com.neusoft.nep.entity.User;
import com.neusoft.nep.entity.nepsFeedback;
import com.neusoft.tools.AlertUtils;
import com.neusoft.tools.FeedbackUtils;
import com.neusoft.tools.UserSession;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.input.MouseButton;
import java.util.List;

public class LogViewController {

    @FXML
    private TableView<nepsFeedback> logTable;
    @FXML
    private TableColumn<nepsFeedback, String> levelColumn;
    @FXML
    private TableColumn<nepsFeedback, String> dateColumn;
    @FXML
    private TableColumn<nepsFeedback, String> provinceColumn;
    @FXML
    private TableColumn<nepsFeedback, String> cityColumn;

    @FXML
    public void initialize() {

        User currentUser = UserSession.getInstance().getLoggedInUser();
        ObservableList<nepsFeedback> data = FXCollections.observableArrayList();
        //  向表单中填充此用户填写的所有反馈记录
        List<nepsFeedback> userFeedback = FeedbackUtils.getFeedbackForUser(currentUser.getPhoneNumber());
        data.addAll(userFeedback);

        levelColumn.setCellValueFactory(cellData ->
                new SimpleStringProperty(cellData.getValue().getLevel())
        );
        dateColumn.setCellValueFactory(cellData ->
                new SimpleStringProperty(cellData.getValue().getDate())
        );
        provinceColumn.setCellValueFactory(cellData ->
                new SimpleStringProperty(cellData.getValue().getProvince())
        );
        cityColumn.setCellValueFactory(cellData ->
                new SimpleStringProperty(cellData.getValue().getCity())
        );

        logTable.setItems(data);
        //  当捕捉到用户双击某一记录的时候，弹出一个有关这条记录详情的弹窗，
        logTable.setOnMouseClicked(event -> {

            if (event.getButton().equals(MouseButton.PRIMARY) && event.getClickCount() == 2) {

                nepsFeedback selectedEntry = logTable.getSelectionModel().getSelectedItem();

                if (selectedEntry != null) {

                    AlertUtils.showFeedbackDetails(selectedEntry);
                }
            }
        });
    }

    /**
     * 退出
     */
    @FXML
    private void exitAction(){
        Platform.exit();
    }
}